package com.acane.instagram.feature.video

import androidx.compose.runtime.mutableStateListOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.acane.instagram.core.model.PostInfo
import com.acane.instagram.core.model.network.datasource.MyRetrofitDatasource
import com.acane.instagram.feature.common.SessionManager
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch

class VideoViewModel : ViewModel() {
    private val _videos = mutableStateListOf<VideoData>()
    val videos: List<VideoData> get() = _videos

    private val _recommendVideo = MutableStateFlow<List<PostInfo>>(emptyList())
    val recommendVideo = _recommendVideo

//    // 使用 MutableStateFlow 和 map 来替代 MutableStateMap
//    private val _isFollowingMap = MutableStateFlow<Map<String, Boolean>>(emptyMap())
//    val isFollowingMap: StateFlow<Map<String, Boolean>> get() = _isFollowingMap
//
//    fun setIsFollowing(userId: String, isFollowing: Boolean) {
//        viewModelScope.launch {
//            _isFollowingMap.value = _isFollowingMap.value.toMutableMap().apply { this[userId] = isFollowing }
//        }
//    }
//
//    fun getIsFollowing(userId: String): Boolean {
//        return _isFollowingMap.value[userId] ?: false
//    }


    init {
        loadVideoData(SessionManager.currentUser?.id)
        // 模拟数据加载
//        _videos.addAll(
//            listOf(
//                VideoData(
//                    id = "1",
//                    url = "${Config.ADDRESS}video/7459e54ea7bff2bc6b413b9cc73e2119.mp4",
//                    likes = 1500
//                ),
//                VideoData(
//                    id = "2",
//                    url = "${Config.ADDRESS}video/e3da975ba4f04a4e6f609c6dfb29d5b6.mp4",
//                    likes = 3200
//                )
//            )
//        )
    }

    fun loadVideoData(userId: String?) {
        viewModelScope.launch {
            _recommendVideo.value = MyRetrofitDatasource.getRecommendVideoList(userId!!).data ?: emptyList()
        }
    }
}